//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: The Get-TabExpansion command.
//
// Creation Date: Dec 31, 2006
//
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Management.Automation;

using BGShell.PowerShell.TabExpansion;
using BGShell.PowerShell.TabExpansion.Handlers;

namespace BGShell.PowerShell.Commands
{
    internal sealed class GetTabExpansionCommand : PSCmdlet
    {
        private string _line;
        private string _word;

        private readonly TabExpander _expander;

        [Parameter(Position = 0, Mandatory = true, HelpMessage = "The command line to expand.")]
        public string Line
        {
            get { return _line; }
            set { _line = value; }
        }

        [Parameter(Position = 1, Mandatory = true, HelpMessage = "The last word of the command line.")]
        public string LastWord
        {
            get { return _word; }
            set { _word = value; }
        }

        public GetTabExpansionCommand()
        {
            _expander = new TabExpander(this);
        }

        protected override void EndProcessing()
        {
            base.EndProcessing();

            WriteObject(_expander.Expand(_line, _word), true);
        }

        internal void WriteExpansions(params string[] expansions)
        {
            _expander.WriteExpansions(expansions);
        }

        internal void WriteExpansions(IEnumerable<String> expansions)
        {
            _expander.WriteExpansions(expansions);
        }

        internal class TabExpander
        {
            private readonly List<TabExpansionHandler> _handlers = new List<TabExpansionHandler>();

            private readonly List<String> _expansions = new List<String>();

            public TabExpander(GetTabExpansionCommand command)
            {
                // _handlers.Add(new ParenthesesExpressionHandler(command));

                _handlers.Add(new StaticExpressionHandler(command));
                _handlers.Add(new VariableExpressionHandler(command));
                _handlers.Add(new TypeNameHandler(command));
                _handlers.Add(new WmiClassHandler(command));
                _handlers.Add(new ParameterNameHandler(command));
                _handlers.Add(new CommandNameHandler(command));
                _handlers.Add(new ItemPathHandler(command));
            }

            public string[] Expand(string line, string word)
            {
                _expansions.Clear();
                
                foreach (TabExpansionHandler handler in _handlers)
                {
                    if (!handler.TryExpand(line, word))
                    {
                        break;
                    }
                }

                _expansions.Sort();

                return _expansions.ToArray();
            }

            public void WriteExpansions(params string[] expansion)
            {
                _expansions.AddRange(expansion);
            }

            public void WriteExpansions(IEnumerable<String> expansions)
            {
                _expansions.AddRange(expansions);
            }
        }
    }
}